﻿using JMI_Nucleo.Classe;
using JMI_Nucleo.Enumerador;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JMI_Nucleo.View.Form_Util
{
    public partial class FrmViewLog : Form
    {
        private DateTime dataInicio = DateTime.MinValue;
        private DateTime dataFinal = DateTime.MinValue;
        private JMIUtil objUtil = null;
        private Log log = null;

        public FrmViewLog()
        {
            InitializeComponent();
        }

        private void BtnFechar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Inicializa()
        {
            objUtil = new JMIUtil();

            dataInicio = objUtil.GetInicioMes();
            dataFinal = objUtil.GetFinalMes();

            log = new Log();

            bsLog.DataSource = log.GetListaLog(dataInicio, dataFinal);
            bsLog.ResetBindings(false);

            rbData.Checked = true;
            rbTipo.Checked = false;
            rbTodo.Checked = false;

            if (bsLog.Count > 0)
            {
                TxtMensagem.DataBindings.Add("Text", bsLog, "Mensagem");
                TxtStacTrace.DataBindings.Add("Text", bsLog, "StackTrace");
            }
        }

        private void FrmViewLog_Load(object sender, EventArgs e)
        {
            Inicializa();
        }

        private void BtnFiltrar_Click(object sender, EventArgs e)
        {
            objUtil = new JMIUtil();

            dataInicio = dtpDataInicio.Value;
            dataFinal = dtpDataFinal.Value;

            log = new Log();

            if (rbData.Checked)
                bsLog.DataSource = log.GetListaLog(dataInicio, dataFinal);
            else if (rbTipo.Checked)
                bsLog.DataSource = log.GetListaLog(TipoErro.Erro);
            else if (rbTodo.Checked)
                bsLog.DataSource = log.GetListaLog(dtpDataInicio.Value, dtpDataFinal.Value, TipoErro.Erro);
            bsLog.ResetBindings(false);
        }       
    }
}
